﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
/// <summary>
/// Summary description for Constant
/// </summary>
public class Constant
{
    
    public static string ROOT_PATH = ConfigurationManager.AppSettings["ROOT_PATH"];
    public static string OWNER = ConfigurationManager.AppSettings["Owner"];
    public static string Dami = ConfigurationManager.AppSettings["dami"];
    public static string ViewIndex = ConfigurationManager.AppSettings["ViewIndex"];//SiteTitle
    public static string View = ConfigurationManager.AppSettings["View"];
    public static string Mota = ConfigurationManager.AppSettings["Mota"];
    public static string strSiteTitle = ConfigurationManager.AppSettings["SiteTitle"];
    public static string email = ConfigurationManager.AppSettings["email"];//TimeCache//LenghtTitle
    public static int lenghtTitle =90;
    public static int time = GetTimeCache();
    public static List<DataLibrary.tbl_TopUrl> list = GetList();
    public static String StrChiTietTinTuc = ConfigurationManager.AppSettings["TinTucChiTiet"];
    public static String StrDownload = ConfigurationManager.AppSettings["TaiVeChiTiet"];
    public static String strSanpham = ConfigurationManager.AppSettings["sanpham"];//ykkh
    public static String strYkkh = ConfigurationManager.AppSettings["ykkh"];//strFaq
    public static String strFaq = ConfigurationManager.AppSettings["Faq"];//strFaq
	public Constant()
	{
	
	}

    public static FriendlyUrl GetItem(String friendlyUrl) {
        FriendlyUrl item = null;
        List<FriendlyUrl> arr = GetListUrl();
        int l = arr.Count;
        for (int i = 0; i < l; i++)
        {
            if (friendlyUrl.ToLower().Trim() == arr[i].FriendUrl.ToLower().Trim() || friendlyUrl.ToLower().Trim() == arr[i].Url.ToLower().Trim())
            {
                item = arr[i];
                break;
            }
        }
        return item;
    }

    public static String FindUrl(String friendlyUrl) {
        String str = friendlyUrl;
        List<FriendlyUrl> arr=GetListUrl();
        int l = arr.Count;
        for (int i = 0; i < l; i++) {
            if (friendlyUrl.ToLower().Trim() == arr[i].FriendUrl.ToLower().Trim()) {
                str = arr[i].Url;
                break;
            }
        }
        return str;
    }

    private static List<FriendlyUrl> GetListUrl() {
        List<FriendlyUrl> list = new List<FriendlyUrl>();
        WebData data = new WebData();
        DataTable tb = data.tbl_Categorys.GetDataTableCache("listUrlFriendLy", 4 * Constant.time, "GetFriendlyUrl",
            CommandType.StoredProcedure);
        foreach (DataRow row in tb.Rows) {
            list.Add(new FriendlyUrl(row["Url"].ToString().Replace("~/","").Replace(".aspx",""), row["FriendlyUrl"].ToString()));

        }

        list.Add(new FriendlyUrl("sanpham", strSanpham));
        list.Add(new FriendlyUrl("New", StrChiTietTinTuc));
        list.Add(new FriendlyUrl("down",StrDownload));
        list.Add(new FriendlyUrl("ykkh", strYkkh));
        list.Add(new FriendlyUrl("Faq", strFaq));
        return list;
    }
    private static int LenghTitle() {
        int len = 90;
        String s = ConfigurationManager.AppSettings["LenghtTitle"].ToString();
        try {
            len = Convert.ToInt32(s);
        }
        catch { }
        return len;
    }
    public static int GetTimeCache(){
        String s=ConfigurationManager.AppSettings["TimeCache"].ToString();
        int t = 30;
        try {
            t = Convert.ToInt32(s);
        }
        catch { 
        }
        return t;
    }
    public static List<DataLibrary.tbl_TopUrl> GetList(){
        WebData db = new WebData();
        return db.tbl_TopUrls.GetListCache("listTitle",4*Constant.time);
    }
    public static String ContentTitle(String str) {
        String s = "";
        str = FindUrl(str);
        DataLibrary.tbl_TopUrl item = GetList().SingleOrDefault(p => p.Url.ToString().ToLower() == str.ToLower());
        if (item != null) {
            s = item.Content_title;
        }
        return s;
    }

    public static String ChangeString(String str) {
        String s = "";
        if (!String.IsNullOrEmpty(str))
        {
            str = str.Trim();
            s = str.Replace(" ", "-").Replace("/", "-").Replace(":", "").Replace(".", "").Replace("?", "");
        }
        return s;
    }
}